﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Christian
 * Datum: 19.06.2010
 * Zeit: 10:05
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */
using System;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace MovieExplorer
{
    /// <summary>
    /// Description of Settings.
    /// </summary>
    public class Settings
    {
        private string _ConfigDirectory;
        public string ConfigDirectory
        {
            get
            {
                if (!_ConfigDirectory.EndsWith(@"\"))
                {
                    return _ConfigDirectory + @"\";
                }
                else
                {
                    return _ConfigDirectory;
                }
            }
            set
            {
                _ConfigDirectory = value;
            }
        }

        private string _MyMoviesDirectory;
        public string MyMoviesDirectory
        {
            get
            {
                if (!_MyMoviesDirectory.EndsWith(@"\"))
                {
                    return _MyMoviesDirectory + @"\";
                }
                else
                {
                    return _MyMoviesDirectory;
                }
            }
            set
            {
                _MyMoviesDirectory = value;
            }
        }

        public string MyMoviesDBFile
        {
            get { return "mymovies"; }
        }

        public string RecordingsDBFile
        {
            get
            {
                if (RecordingServiceUsed)
                {
                    return "SVCDatabase";
                }
                else
                {
                    return "TVDatabase";
                }
            }
        }

        public bool RecordingServiceUsed { get; set; }
        public bool DBUsed { get; set; }
        public bool SQLiteV3MyMovies { get; set; }
        public bool SQLiteV3Recordings { get; set; }
        public bool UTF8 { get; set; }
        public int BackdropImageCount { get; set; }

        public Settings()
        {
            // default values
            ConfigDirectory = "";
            MyMoviesDirectory = "";
            BackdropImageCount = -1;
            RecordingServiceUsed = false;
            DBUsed = false;
            SQLiteV3MyMovies = false;
            SQLiteV3Recordings = false;
            UTF8 = false;
        }

        public void Load(string filename)
        {
            if (!File.Exists(filename))
                return;

            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filename);

                XmlNode nodeSettings = doc.SelectSingleNode("settings");

                if (nodeSettings != null)
                {
                    XmlNode nodeStandard = nodeSettings.SelectSingleNode("standard");

                    if (nodeStandard != null)
                    {
                        ConfigDirectory = nodeStandard.Attributes.GetNamedItem("configDirectory").Value;
                        MyMoviesDirectory = nodeStandard.Attributes.GetNamedItem("mymoviesDirectory").Value;
                        BackdropImageCount = int.Parse(nodeStandard.Attributes.GetNamedItem("backdropCount").Value);
                    }

                    XmlNode nodeDB = nodeSettings.SelectSingleNode("db");

                    if (nodeDB != null)
                    {
                        DBUsed = Boolean.Parse(nodeDB.Attributes.GetNamedItem("dbUsed").Value);
                        SQLiteV3MyMovies = Boolean.Parse(nodeDB.Attributes.GetNamedItem("SQLiteV3Movies").Value);
                        SQLiteV3Recordings = Boolean.Parse(nodeDB.Attributes.GetNamedItem("SQLiteV3Recordings").Value);
                        UTF8 = Boolean.Parse(nodeDB.Attributes.GetNamedItem("UTF8").Value);
                        RecordingServiceUsed = Boolean.Parse(nodeDB.Attributes.GetNamedItem("RecordingService").Value);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void Save(string filename)
        {
            try
            {
                XmlDocument doc = new XmlDocument();

                XmlNode nodeSettings = doc.CreateElement("settings");

                if (nodeSettings != null)
                {
                    doc.AppendChild(nodeSettings);

                    XmlNode nodeStandard = doc.CreateElement("standard");

                    if (nodeStandard != null)
                    {
                        nodeSettings.AppendChild(nodeStandard);

                        nodeStandard.Attributes.Append(doc.CreateAttribute("configDirectory")).Value = ConfigDirectory;
                        nodeStandard.Attributes.Append(doc.CreateAttribute("mymoviesDirectory")).Value = MyMoviesDirectory;
                        nodeStandard.Attributes.Append(doc.CreateAttribute("backdropCount")).Value = BackdropImageCount.ToString();
                    }

                    XmlNode nodeDB = doc.CreateElement("db");

                    if (nodeDB != null)
                    {
                        nodeSettings.AppendChild(nodeDB);

                        nodeDB.Attributes.Append(doc.CreateAttribute("dbUsed")).Value = DBUsed.ToString();
                        nodeDB.Attributes.Append(doc.CreateAttribute("SQLiteV3Movies")).Value = SQLiteV3MyMovies.ToString();
                        nodeDB.Attributes.Append(doc.CreateAttribute("SQLiteV3Recordings")).Value = SQLiteV3Recordings.ToString();
                        nodeDB.Attributes.Append(doc.CreateAttribute("UTF8")).Value = UTF8.ToString();
                        nodeDB.Attributes.Append(doc.CreateAttribute("RecordingService")).Value = RecordingServiceUsed.ToString();
                    }
                }

                doc.Save(filename);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
